// pages/my/idCardAuth/idCardAuth.js
import { RES_HOST } from "../../../utils/const"
import http from "../../../utils/http"
import util from "../../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    res_host: RES_HOST,
    no_login_follow: true,
    no_follow: false,
    delBtnWidth: 180,
    //关注列表
    followList: [],
    //显示关注列表
    isFollowList: false,
    //未完成数据
    list: [],
    isEvent: true,
    oldItem: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // let isLoginStatus = wx.getStorageSync("isLoginStatus") || false;
    // let phoneNum = wx.getStorageSync("phoneNum") || 0;
    // let token = wx.getStorageSync("token") || null;
    // let that = this;
    // this.setData({
    //   no_login_follow: !isLoginStatus,
    //   token: token
    // })
    // if (isLoginStatus && token) {
    //   //获取的关注的列表
    //   that.initFollowList();
    //   //获取可以的宽度
    //   that.initEleWidth();
    // } else {
    //   this.setData({
    //     no_login_follow: true,
    //     no_follow: false
    //   })
    // }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onShow: function () {
    let isLoginStatus = wx.getStorageSync("isLoginStatus") || false;
    let phoneNum = wx.getStorageSync("phoneNum") || 0;
    let token = wx.getStorageSync("token") || null;
    let that = this;
    this.setData({
      no_login_follow: !isLoginStatus,
      token: token
    })
    if (isLoginStatus && token) {
      //获取的关注的列表
      that.initFollowList();
      //获取可以的宽度
      that.initEleWidth();
    } else {
      this.setData({
        no_login_follow: true,
        no_follow: false
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  },
  /**
  * 获取关注的列表
  */
  initFollowList: function (e) {
    var that = this;
    http({
      url: "/attention/list",
      method: "POST",
      success: function (res) {
        if (res.code == 200) {
          if (res.data.length) {
            that.setFollowList(res.data);
            that.setData({
              no_follow: false,
              isFollowList: true
            });
          } else {
            that.setData({
              no_follow: true
            });
          }
        } else if (res.code == 403) {
          wx.showModal({
            showCancel: false,
            content: res.msg,
            success: function (res) {
              if (res.confirm) {
                wx.clearStorageSync("token")
                wx.clearStorageSync("isLoginStatus");
                wx.clearStorageSync("phoneNum");
                wx.navigateTo({
                  url: '../login/login?url=/pages/my/index/index'
                })
              }
            }
          })
        } else {
          wx.showModal({
            showCancel: false,
            content: res.msg,
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '../login/login?url=/pages/my/index/index'
                })
              }
            }
          })
        }
      }
    })
  },
  //初始化关注列表数据
  setFollowList: function (data) {
    var that = this;
    var arr = [];
    data.forEach(function (val) {
      val.txtStyle = '';
      val.publishTime = util.getMonthDay(val.publishTime);
      arr.push(val);
    });
    that.setData({
      followList: arr
    });
    console.log(this.data.followList);
  },
  //点击去登录按钮
  goLogin: function (e) {
    wx.navigateTo({
      url: '../login/login?url=/pages/my/index/index'
    })
  },
  //开始滑动
  touchS: function (e) {
    if (e.touches.length == 1) {
      this.setData({
        //设置触摸起始点水平方向位置
        startX: e.touches[0].clientX
      });
    }
  },
  touchM: function (e) {
    if (e.touches.length == 1) {
      //手指移动时水平方向位置
      var moveX = e.touches[0].clientX;
      //手指起始点位置与移动期间的差值
      var disX = this.data.startX - moveX;
      var delBtnWidth = this.data.delBtnWidth;
      var txtStyle = "";
      if (disX == 0 || disX < 0) {//如果移动距离小于等于0，文本层位置不变
        txtStyle = "left:0px";
      } else if (disX > 0) {//移动距离大于0，文本层left值等于手指移动距离
        txtStyle = "left:-" + disX + "px";
        if (disX >= delBtnWidth) {
          //控制手指移动距离最大值为删除按钮的宽度
          txtStyle = "left:-" + delBtnWidth + "px";
        }
      }
      //获取手指触摸的是哪一项
      var index = e.currentTarget.dataset.index;
      var list = this.data.followList;
      if (index >= 0) {
        list[index].txtStyle = txtStyle;
        //更新列表的状态
        this.setData({
          followList: list
        });
      }
    }
  },
  touchE: function (e) {
    if (e.changedTouches.length == 1) {
      var that = this;
      //手指移动结束后水平位置
      var endX = e.changedTouches[0].clientX;
      //触摸开始与结束，手指移动的距离
      var disX = this.data.startX - endX;
      var delBtnWidth = this.data.delBtnWidth;
      //如果距离小于删除按钮的1/2，不显示删除按钮
      var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "px" : "left:0px";
      //获取手指触摸的是哪一项
      var index = e.currentTarget.dataset.index;
      var list = this.data.followList;
      this.setData({
        oldItem: index
      })
      if (index >= 0) {
        list[index].txtStyle = txtStyle;
        console.log("------txtStyle", txtStyle)
        //更新列表的状态
        //判断点击触发注意事项
        list.forEach(function (v, index) {
          if (index != that.data.oldItem) {
            v.txtStyle = "left:0px"
          }
        })
        let str = txtStyle.substring(5);
        let reg = /^\-+/g;
        reg.lastIndex = 0;
        if (reg.test(str)) {
          this.setData({
            isEvent: false
          })
        } else {
          setTimeout(function () {
            that.setData({
              isEvent: true
            })
          }, 600)

        }
        this.setData({
          followList: list
        });
      }
    }
  },
  //获取元素自适应后的实际宽度
  getEleWidth: function (w) {
    var real = 0;
    try {
      var res = wx.getSystemInfoSync().windowWidth;
      var scale = (750 / 2) / (w / 2);//以宽度750px设计稿做宽度的自适应
      // console.log(scale);
      real = Math.floor(res / scale);
      return real;
    } catch (e) {
      return false;
      // Do something when catch error
    }
  },
  initEleWidth: function () {
    var delBtnWidth = this.getEleWidth(180);
    this.setData({
      delBtnWidth: delBtnWidth
    });
  },
  //点击删除按钮事件
  delItem: function (e) {
    var that = this;
    //获取列表中要删除项的下标
    let id = e.currentTarget.dataset.id;
    let list = this.data.followList;
    let num = this.delNum(list, id);
    http({
      url: `/attention/${id}/0`,
      method: "POST",
      data: {
        preProcessId: id,
        status: 0
      },
      success: function (res) {
        if (res.code == 200) {
          // 移除列表中下标为index的项
          list.splice(num, 1);
          // 更新列表的状态
          that.setData({
            followList: list
          });
        } else {
          //取消关注失败后的方法
        }
      }
    })

  },
  // 获取选中的号码
  delNum: function (data, target) {
    var num = '';
    data.forEach(function (val, index) {
      if (val.id == target) {
        num = index;
      }
    })
    return num;
  },
  //点击关注跳转首页列表
  lookFang: function (e) {
    let id = e.currentTarget.dataset.preid;
    let status = e.currentTarget.dataset.status;
    console.log("isEvent",this.data.isEvent);
    if (this.data.isEvent){
      if (e.type == 'tap' && status== '1') {
        wx.navigateTo({
          url: '/pages/index/propertyDetailA/propertyDetailA?preId=' + id + '&isShared=no'
        })
      } else if (e.type == 'tap' && status == '0') {
        wx.showToast({
          title: '已下架',
          icon: "none"
        })
      }
    }else{
      return false;
    }
  }
})